/**
  ******************************************************************************
  * @file    User/user.c
  * @author  JUSHENG Application Team
  * @version V1.0.4
  * @date    12-14-2021
  * @brief   Main program body
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; COPYRIGHT 2021 JUSHENG</center></h2>
  *
  *
  ******************************************************************************
  */ 

/* Includes ------------------------------------------------------------------*/
#include "include.h"
#include "user.h"

/** @addtogroup Template_Project
  * @{
  */
  
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Uart baud rate                            */
#define UART_BAUDRATE                   LL_UARTx_BAUDRATE                  

/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Public variables ---------------------------------------------------------*/
u8 flash_data_buf[500] __attribute__((aligned(4)));

/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/


/**
  * @brief  Uart initialization function
  * @param  None
  * @retval None
  */
void uart_init(void)
{
    TYPE_LL_GPIO_INIT gpio_struct;
    /* UART configuration structure variable     */
    TYPE_LL_UART_INIT     uart_cfg;
    memset(&gpio_struct,0x00,sizeof(gpio_struct));
    /* Initialize the relevant variables of the UART */
    memset(&uart_cfg, 0x0, sizeof(uart_cfg));

    /* pb10 func1   rx   fpga:v18 pb11 func1   tx   fpga:u16 */
    gpio_struct.gpio_pin   = LL_GPIO_PIN_11;
    gpio_struct.gpio_mode  = LL_GPIO_MODE_AF;
    ll_gpio_init(GPIOA, &gpio_struct);
    ll_gpio_pin_af_config(GPIOA, LL_GPIO_PIN_SOURCE_11, LL_GPIO_AF_0);
    /* Configure UART baud rate to UART_BAUDRATE        */
    uart_cfg.baudrate       = UART_BAUDRATE;
    uart_cfg.bit_width_sel  = LL_UART_WORD_LENGTH_8B;
    uart_cfg.parity         = LL_UART_PARITY_NO;
    uart_cfg.stop_bit_sel   = LL_UART_STOP_1B;
    /* Call the UART driver configuration function */
    ll_uart_init(UART0, &uart_cfg);
    debug_select_interface_set(DEBUG_UART0);
    ll_uart_enable(UART0);
    delay_ms(1);
}


/**
  * @brief  eflash initialization function
  * @param  None
  * @retval None
  */
void eflash_init(void)
{
    TYPE_LL_EFLASH_INIT eflash_init;
    memset(&eflash_init, 0x00, sizeof(eflash_init));
    
    /* Initialize the relevant variables  */
    ll_eflash_init(EFLASH, &eflash_init);
}

/**
  * @brief  eflash data handle function
  * @param  None
  * @retval None
  */
void eflash_handle(void)
{
    u32 i = 0;
    u32 *pBuf = NULL;
    u8 eflash_data_nvr_sec_index = 0;      //NVR_secotr0
    u32 eflash_addr = eflash_data_nvr_sec_index * LL_EF_SECTOR_SIZE;
    pBuf = (u32 *)&flash_data_buf[0];
    printf("eflash nvr program begin \r\n");
    memset(flash_data_buf, 0x00,sizeof(flash_data_buf));
    for(i = 0; i < (sizeof(flash_data_buf)); i += 4)
    {
        *pBuf++ = LL_EFLASH_DATA_NVR_GET(eflash_addr + i);
    }
    printf("flash nvr begin read:%d bytes\r\n", sizeof(flash_data_buf));
    printf_array(flash_data_buf, sizeof(flash_data_buf));
    printf("\r\n");
    ll_eflash_erase_one_sector_nvr(EFLASH, eflash_data_nvr_sec_index);   //erase nvr secotr
    
    memset(flash_data_buf, 0x00,sizeof(flash_data_buf));
    for(i = 0; i < LL_EF_SECTOR_SIZE; i += 4)
    {
        if(0xFFFFFFFF != LL_EFLASH_DATA_NVR_GET(eflash_addr + i))        //get nvr data (4 byte)
        {
            printf("eflash_nvr erase verify failed\r\n");
            break;
        }
    }
    if(i >= LL_EF_SECTOR_SIZE)
    {
        printf("eflash nvr erase ok\r\n");
        for(i = 0; i < sizeof(flash_data_buf); i++)
        {
            flash_data_buf[i] = i;
        }
        pBuf = (u32 *)&flash_data_buf[0];
        for(i = 0; i < (sizeof(flash_data_buf)); i += 4)
        {
            ll_eflash_prog_one_data_nvr(EFLASH, eflash_addr + i, *pBuf++);    //program one data(4 byte)
        }
        printf("flash nvr write:%d bytes\r\n", sizeof(flash_data_buf));
        printf_array(flash_data_buf, sizeof(flash_data_buf));
        printf("\r\n");
        memset(flash_data_buf, 0x00, sizeof(flash_data_buf));
        pBuf = (u32 *)&flash_data_buf[0];
        for(i = 0; i < (sizeof(flash_data_buf)); i += 4)
        {
            *pBuf++ = LL_EFLASH_DATA_NVR_GET(eflash_addr + i);
        }
        printf("flash nvr read after write:%d bytes\r\n", sizeof(flash_data_buf));
        printf_array(flash_data_buf, sizeof(flash_data_buf));
        printf("\r\n");
    }
    while(1);
}

/**
  * @brief  eflash detele init function
  * @param  None
  * @retval None
  */
void eflash_deinit(void)
{

    
}



/**
  * @}
  */

/*************************** (C) COPYRIGHT 2021 JUSHENG ***** END OF FILE *****/
